﻿using CommonCater;
using Lufei_CaterBLL;
using Lufei_CaterModel;
using Lufei_CaterUI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ItcastCater
{
    public partial class FrmRoom : Form
    {
        public FrmRoom()
        {
            InitializeComponent();
        }

        private void FrmRoom_Load(object sender, EventArgs e)
        {
            LoadRoomInfo();
            LoadDeskInfo();
        }

        DeskInfoBLL bll = new DeskInfoBLL();


        /// <summary>
        /// 加载餐桌信息
        /// </summary>
        private void LoadDeskInfo()
        {

            dgvDeskInfo.AutoGenerateColumns = false;
            List<DeskInfo> deskInfos=  bll.GetDesks();

         
            dgvDeskInfo.DataSource = deskInfos;
            ShowDeskState(deskInfos);

            dgvDeskInfo.SelectedRows[0].Cells[0].Selected = false;
         

        }

        /// <summary>
        /// 展示餐桌状态
        /// </summary>
        private void ShowDeskState(List<DeskInfo> deskInfos)
        {

            //获取所有的餐桌状态

            for (int i = 0; i < deskInfos.Count; i++)
            {
                if (deskInfos[i].DeskState == 1)
                {
                    dgvDeskInfo.Rows[i].Cells["Column9"].Value = "正在使用";
                }
                else if (deskInfos[i].DeskState == 0)
                {
                    dgvDeskInfo.Rows[i].Cells["Column9"].Value = "空闲";
                }
            }
            log<FrmRoom>.LogDebug("展示餐桌状态");          
        }



        /// <summary>
        /// 删除桌子
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDelete_Click(object sender, EventArgs e)
        {
            //判断是否被选中
            if (dgvDeskInfo.SelectedRows.Count>0)
            {
                //获取选中的id
                int id = Convert.ToInt32(dgvDeskInfo.SelectedRows[0].Cells[0].Value);
                bool isDeleted=  bll.DeleteDeskByID(id);
                if (isDeleted)
                {
                    MessageBox.Show("删除成功!!!!!");
                    LoadDeskInfo();//回显数据
                    log<FrmRoom>.LogDebug("删除成功!!!!!");

                }
                else
                {
                    MessageBox.Show("删除失败!!!!!");
                    LoadDeskInfo();//回显数据
                    log<FrmRoom>.LogDebug("删除失败!!!!!");

                }
            }
            else
            {
                MessageBox.Show("请选择桌子!!!!!");
                log<FrmRoom>.LogDebug("没有选择桌子!!!!!");
            }
        }


        /// <summary>
        /// 打开桌子窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAddDesk_Click(object sender, EventArgs e)
        {
            FrmDeskAdd frmDeskAdd = new FrmDeskAdd();
            frmDeskAdd.FormClosed += FrmDeskAdd_FormClosed;
            frmDeskAdd.ShowDialog();
        }

        private void FrmDeskAdd_FormClosed(object sender, FormClosedEventArgs e)
        {
            LoadDeskInfo();
        }


        //窗体传值
        private event EventHandler My_Event_FrmChangeDesk;

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnUpdateDesk_Click(object sender, EventArgs e)
        {

            //判断当前是否选择了列
            if (dgvDeskInfo.SelectedRows.Count > 0)
            {
                //包装参数对象
                UpdateMemmberInfo<DeskInfo> des = new UpdateMemmberInfo<DeskInfo>();

                //获取id
                int id = Convert.ToInt32(dgvDeskInfo.SelectedRows[0].Cells[0].Value);

                //从bll获取对象
                DeskInfo rm = bll.GetDeskInfoByID(id);

                des.obj = rm;
                des.Type = 1;

                 //窗体传值
                 FrmChangeDesk frmChangeDesk = new FrmChangeDesk();
                this.My_Event_FrmChangeDesk += frmChangeDesk.SetValue;
                if (this.My_Event_FrmChangeDesk != null)
                {
                    this.My_Event_FrmChangeDesk(this, des);
                }
                frmChangeDesk.FormClosed += FrmChangeDesk_FormClosed;
                frmChangeDesk.ShowDialog();
            }
            else
            {
                MessageBox.Show("请选择房间!!!!!");
                log<FrmRoom>.LogDebug("没有选择房间!!!!!");
            }
        }

        private void FrmChangeDesk_FormClosed(object sender, FormClosedEventArgs e)
        {
            LoadDeskInfo();
        }



        #region 房间操作

        RoomInfoBLL roomInfoBLL = new RoomInfoBLL();
        /// <summary>
        /// 加载所有房间
        /// </summary>
        private void LoadRoomInfo()
        {
            //获取所有没有被删除的房间信息
        

            List<RoomInfo> roomInfos=   roomInfoBLL.GetRoomsByDelFlag();

            //取消自动生成列
            dgvRoomInfo.AutoGenerateColumns = false;
            dgvRoomInfo.DataSource= roomInfos;
            dgvRoomInfo.SelectedRows[0].Cells[0].Selected = false;
        }

        private void btnAddRoom_Click(object sender, EventArgs e)
        {
            FromAddRoom room = new FromAddRoom();
            room.FormClosed += Room_FormClosed;        
            room.ShowDialog();
        }

        private void Room_FormClosed(object sender, FormClosedEventArgs e)
        {
            LoadRoomInfo();
        }

        /// <summary>
        /// 删除房间
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDeleteRoom_Click(object sender, EventArgs e)
        {
            //判断当前是否选择了列
            if (dgvRoomInfo.SelectedRows.Count > 0)
            {
                //获取选中的id
                int id = Convert.ToInt32(dgvRoomInfo.SelectedRows[0].Cells[0].Value);
                bool isDeleted= roomInfoBLL.DeleteRoom(id);
                if (isDeleted)
                {
                    MessageBox.Show("删除成功!!!!!");
                    LoadRoomInfo();//回显数据
                    log<FrmRoom>.LogDebug("删除成功!!!!!");
                }
                else
                {
                    MessageBox.Show("删除失败!!!!!");
                    LoadRoomInfo();//回显数据
                    log<FrmRoom>.LogDebug("删除失败!!!!!");
                }
            }
            else
            {
                MessageBox.Show("请选择房间!!!!!");
                log<FrmRoom>.LogDebug("没有选择房间!!!!!");
            }
        }


        //窗体传值
        private event EventHandler My_Event_FrmChangeRoom;

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            //判断当前是否选择了列
            if (dgvRoomInfo.SelectedRows.Count > 0)
            {
                //包装参数对象
                UpdateMemmberInfo<RoomInfo> roomInfo = new UpdateMemmberInfo<RoomInfo>();
                //获取id
                int id = Convert.ToInt32(dgvRoomInfo.SelectedRows[0].Cells[0].Value);
                //从bll获取对象
                RoomInfo rm= roomInfoBLL.GetRoomByID(id);

                roomInfo.obj = rm;

                //窗体传值
                FrmChangeRoom frmChange=new FrmChangeRoom();
                this.My_Event_FrmChangeRoom += frmChange.SetValue;
                if (this.My_Event_FrmChangeRoom!=null)
                {
                    this.My_Event_FrmChangeRoom(this, roomInfo);
                }
                frmChange.FormClosed += FrmChange_FormClosed;
                frmChange.ShowDialog();

            }
            else
            {
                MessageBox.Show("请选择房间!!!!!");
                log<FrmRoom>.LogDebug("没有选择房间!!!!!");
            }
        }

        private void FrmChange_FormClosed(object sender, FormClosedEventArgs e)
        {
            LoadRoomInfo();
        }




        #endregion

    }
}
